<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebRTC 测试客户端</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            text-align: center;
        }
        .content {
            padding: 20px;
        }
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            background: #fafafa;
        }
        .section h3 {
            margin-top: 0;
            color: #333;
            border-bottom: 2px solid #667eea;
            padding-bottom: 10px;
        }
        .controls {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            margin-bottom: 20px;
        }
        button {
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        .btn-primary {
            background: #667eea;
            color: white;
        }
        .btn-primary:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
        }
        .btn-success {
            background: #28a745;
            color: white;
        }
        .btn-success:hover {
            background: #218838;
        }
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        .btn-danger:hover {
            background: #c82333;
        }
        .btn-warning {
            background: #ffc107;
            color: #212529;
        }
        .btn-warning:hover {
            background: #e0a800;
        }
        button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none !important;
        }
        .status {
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-weight: 500;
        }
        .status.connecting {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
        }
        .status.connected {
            background: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .status.disconnected {
            background: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .video-container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }
        video {
            width: 100%;
            height: 300px;
            background: #000;
            border-radius: 8px;
            border: 2px solid #ddd;
        }
        .log-container {
            height: 400px;
            overflow-y: auto;
            background: #1e1e1e;
            color: #f8f8f2;
            padding: 15px;
            border-radius: 6px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            line-height: 1.4;
        }
        .log-entry {
            margin-bottom: 5px;
            padding: 2px 0;
        }
        .log-entry.info {
            color: #66d9ef;
        }
        .log-entry.warn {
            color: #fd971f;
        }
        .log-entry.error {
            color: #f92672;
        }
        .log-entry.debug {
            color: #75715e;
        }
        .log-entry.success {
            color: #a6e22e;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }
        .stat-card {
            background: white;
            padding: 15px;
            border-radius: 6px;
            border: 1px solid #e0e0e0;
            text-align: center;
        }
        .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: #667eea;
        }
        .stat-label {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .input-group {
            display: flex;
            gap: 10px;
            align-items: center;
            margin-bottom: 15px;
        }
        input[type="text"] {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        label {
            font-weight: 500;
            color: #333;
            min-width: 120px;
        }
        .progress-bar {
            width: 100%;
            height: 6px;
            background: #e0e0e0;
            border-radius: 3px;
            overflow: hidden;
            margin: 10px 0;
        }
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #667eea, #764ba2);
            width: 0%;
            transition: width 0.3s ease;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🌐 WebRTC 连接测试工具</h1>
            <p>测试服务器环境对WebRTC的支持，包含详细的连接诊断</p>
        </div>
        
        <div class="content">
            <!-- 连接配置 -->
            <div class="section">
                <h3>📡 连接配置</h3>
                <div class="input-group">
                    <label>服务器地址:</label>
                    <input type="text" id="serverUrl" value="ws://localhost:8080/ws" placeholder="输入WebSocket服务器地址">
                </div>
                <div class="input-group">
                    <label>启用STUN:</label>
                    <input type="checkbox" id="enableStun" checked style="width: auto; margin-right: 10px;">
                    <span style="color: #666; font-size: 12px;">取消勾选将仅使用host候选（适合局域网测试）</span>
                </div>
                <div class="input-group" id="stunServerGroup">
                    <label>STUN服务器:</label>
                    <input type="text" id="stunServer" value="stun:stun.l.google.com:19302,stun:stun1.l.google.com:19302" placeholder="输入STUN服务器地址（多个用逗号分隔）">
                </div>
                <div class="input-group">
                    <label>预设配置:</label>
                    <select id="stunPreset" style="padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;">
                        <option value="google">Google STUN (默认)</option>
                        <option value="cloudflare">Cloudflare STUN</option>
                        <option value="custom">自定义</option>
                    </select>
                </div>
                <div class="controls">
                    <button id="connectBtn" class="btn-primary">🔗 连接服务器</button>
                    <button id="disconnectBtn" class="btn-danger" disabled>❌ 断开连接</button>
                    <button id="startTestBtn" class="btn-success" disabled>▶️ 开始WebRTC测试</button>
                    <button id="stopTestBtn" class="btn-warning" disabled>⏹️ 停止测试</button>
                    <button id="clearLogBtn" class="btn-warning">🗑️ 清空日志</button>
                </div>
            </div>

            <!-- 连接状态 -->
            <div class="section">
                <h3>📊 连接状态</h3>
                <div id="connectionStatus" class="status disconnected">
                    ❌ 未连接
                </div>
                <div class="progress-bar">
                    <div id="progressFill" class="progress-fill"></div>
                </div>
                <div class="stats-grid">
                    <div class="stat-card">
                        <div id="wsStatus" class="stat-value">❌</div>
                        <div class="stat-label">WebSocket状态</div>
                    </div>
                    <div class="stat-card">
                        <div id="pcStatus" class="stat-value">❌</div>
                        <div class="stat-label">PeerConnection状态</div>
                    </div>
                    <div class="stat-card">
                        <div id="iceStatus" class="stat-value">❌</div>
                        <div class="stat-label">ICE连接状态</div>
                    </div>
                    <div class="stat-card">
                        <div id="mediaStatus" class="stat-value">❌</div>
                        <div class="stat-label">媒体流状态</div>
                    </div>
                </div>
            </div>

            <!-- 视频显示 -->
            <div class="section">
                <h3>📹 视频流</h3>
                <div class="video-container">
                    <div>
                        <h4>本地视频 (如果有摄像头)</h4>
                        <video id="localVideo" autoplay muted playsinline></video>
                    </div>
                    <div>
                        <h4>远程视频 (服务器推送)</h4>
                        <video id="remoteVideo" autoplay playsinline></video>
                    </div>
                </div>
            </div>

            <!-- 详细日志 -->
            <div class="section">
                <h3>📝 详细日志</h3>
                <div id="logContainer" class="log-container">
                    <div class="log-entry info">[系统] WebRTC测试工具已加载</div>
                </div>
            </div>
        </div>
    </div>

    <script src="webrtc_client.js"></script>
</body>
</html>